﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization.Json;

namespace CP.Utility
{ 
    public class JsonHelper
    {
        //对象转json
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string Obj_Json(object obj)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            // 定义一个stream用来存发序列化之后的内容
            Stream stream = new MemoryStream();
            serializer.WriteObject(stream, obj);
            // 从头到尾将stream读取成一个字符串形式的数据，并且返回
            stream.Position = 0;
            StreamReader streamReader = new StreamReader(stream);
            return streamReader.ReadToEnd();
        }
        //json转对象
        /// <summary>
        /// 
        /// </summary>
        /// <param name="json"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static Object Json_Obj(String json, Type t)
        {
            try
            {
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(t);
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
                {

                    return serializer.ReadObject(ms);
                }
            }
            catch
            {
                return null;
            }
        }

        //对象转json
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="U"></typeparam>
        /// <param name="jsonName"></param>
        /// <param name="IL"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string ObjectListToJson<T>(string jsonName, IList<T> IL, int count)
        {
            StringBuilder Json = new StringBuilder();
            Json.Append("{\"" + jsonName + "\":[");
            if (IL.Count > 0)
            {
                for (int i = 0; i < IL.Count; i++)
                {
                    T obj = Activator.CreateInstance<T>();
                    Type type = obj.GetType();
                    PropertyInfo[] pis = type.GetProperties();
                    Json.Append("{");
                    for (int j = 0; j < pis.Length; j++)
                    {
                        Json.Append("\"" + pis[j].Name.ToString() + "\":\"" + pis[j].GetValue(IL[i], null) + "\"");
                        if (j < pis.Length - 1)
                        {
                            Json.Append(",");
                        }
                    }
                    Json.Append("}");
                    if (i < IL.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("],\"count\":" + count + "}");
            return Json.ToString();
        }
        //Json转对象
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static IList<object> JsonToObjectList(string obj, Type t)
        {
            IList<object> _Test = new List<object>();

            DataContractJsonSerializer _Json = new DataContractJsonSerializer(t);
            byte[] _Using = System.Text.Encoding.UTF8.GetBytes(obj);
            System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_Using);
            _MemoryStream.Position = 0;

            _Test = (List<object>)_Json.ReadObject(_MemoryStream);

            return _Test;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static object[] Obj2Json(object[] parameters)
        {
            List<object> newlist = new List<object>();
            foreach (object param in parameters)
            {
                object temp = param;
                if (param.GetType().IsClass)
                {
                    temp = Obj_Json(param);
                } 

                newlist.Add(temp);
            }
            return newlist.ToArray();
        }
        /// <summary>
        /// sss
        /// </summary>
        /// <param name="parameters"></param>
        /// <param name="ptypes"></param>
        /// <returns></returns>
        public static object[] Json2Obj(object[] parameters, ParameterInfo[] ptypes)
        {
            List<object> newparams = new List<object>();

            for (int i = 0; i < parameters.Length; i++)
            {
                object temp = parameters[i];

                temp = Json_Obj(parameters[i].ToString(), ptypes[i].ParameterType);
                newparams.Add(temp);
            }
            return newparams.ToArray();
        }


    }
}
